using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The StandarLevelSubjectTableDB class is responsible for interacting with the database to retrieve and store information
/// about StandarLevelSubjectTable objects.
/// </summary>
public class StandarLevelSubjectTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of StandarLevelSubjectTable from the underlying datasource.
/// </summary>
/// <param name="subjectKey">The unique SubjectKey of the StandarLevelSubjectTable in the database.</param>
/// <returns>An StandarLevelSubjectTable when the SubjectKey was found in the database, or null otherwise.</returns>
public static StandarLevelSubjectTable GetItem(int subjectKey)
{
StandarLevelSubjectTable myStandarLevelSubjectTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStandarLevelSubjectTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@subjectKey", subjectKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myStandarLevelSubjectTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myStandarLevelSubjectTable;
}
}

/// <summary>
/// Returns a list with StandarLevelSubjectTable objects.
/// </summary>
/// <returns>A generics List with the StandarLevelSubjectTable objects.</returns>
public static StandarLevelSubjectTableList GetList()
{
StandarLevelSubjectTableList tempList = new StandarLevelSubjectTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStandarLevelSubjectTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a StandarLevelSubjectTable in the database.
/// </summary>
/// <param name="myStandarLevelSubjectTable">The StandarLevelSubjectTable instance to save.</param>
/// <returns>The new SubjectKey if the StandarLevelSubjectTable is new in the database or the existing SubjectKey when an item was updated.</returns>
public static int Save(StandarLevelSubjectTable myStandarLevelSubjectTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStandarLevelSubjectTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myStandarLevelSubjectTable.SubjectKey == -1)
{myCommand.Parameters.AddWithValue("@subjectKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@subjectKey", myStandarLevelSubjectTable.SubjectKey);
}
if (string.IsNullOrEmpty(myStandarLevelSubjectTable.Remark))
{
myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remark", myStandarLevelSubjectTable.Remark);
}
if (myStandarLevelSubjectTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myStandarLevelSubjectTable.Status);
}
if (myStandarLevelSubjectTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myStandarLevelSubjectTable.CreatedDateTime);
}
if (myStandarLevelSubjectTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myStandarLevelSubjectTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a StandarLevelSubjectTable from the database.
/// </summary>
/// <param name="subjectKey">The SubjectKey of the StandarLevelSubjectTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int subjectKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStandarLevelSubjectTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@subjectKey", subjectKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the StandarLevelSubjectTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static StandarLevelSubjectTable FillDataRecord(IDataRecord myDataRecord )
{
StandarLevelSubjectTable myStandarLevelSubjectTable = new StandarLevelSubjectTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandardLevelKey")))
{
myStandarLevelSubjectTable.StandardLevelKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StandardLevelKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SubjectKey")))
{
myStandarLevelSubjectTable.SubjectKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SubjectKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
{
myStandarLevelSubjectTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myStandarLevelSubjectTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myStandarLevelSubjectTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myStandarLevelSubjectTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myStandarLevelSubjectTable;
}
}

 } 
